import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:screen_state/screen_state.dart';
import 'package:permission_handler/permission_handler.dart';

class MyWebController extends GetxController with WidgetsBindingObserver {
  RxString url = "".obs;

  final Screen _screen = Screen();

  RxBool isLoading = true.obs;

  @override
  void onInit() {
    super.onInit();
    url.value = "https://wukongmiliao.com/";
    update();
    requestPermissions();
    // 添加生命周期观察者
    WidgetsBinding.instance.addObserver(this);
    _screen.screenStateStream.listen((ScreenStateEvent event) {
      if (event == ScreenStateEvent.SCREEN_OFF) {
        //
        print("已经锁屏了");
      }
    });
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    // 移除生命周期观察者
    WidgetsBinding.instance.removeObserver(this);
  }

  void changeIsLoading() {
    isLoading.value = false;
    update();
  }

  // 申请权限
  Future<void> requestPermissions() async {
    var cameraStatus = await Permission.camera.request();
    var microphoneStatus = await Permission.microphone.request();
    print("object");
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.paused) {
      // 应用切换到后台
      Get.back();
    } else if (state == AppLifecycleState.resumed) {
      // 应用从后台返回前台
    } else if (state == AppLifecycleState.inactive) {
      // 应用处于非活动状态
      Get.back();
    }
    super.didChangeAppLifecycleState(state);
  }
}
